<?php

namespace cms\modules\v1\controllers;

use cms\modules\v1\models\access\ModuleActions;
use cms\modules\v1\models\user\User;
use cms\modules\v1\models\user\UserBusiness;
use cms\modules\v1\models\user\UserCancellation;
use cms\modules\v1\models\user\UserLoginLog;
use cms\modules\v1\models\user\UserUnusualLog;
use cms\modules\v1\models\userLevel\UserLevel;
use cms\modules\v1\models\userReceipt\ReceiptBank;
use common\models\Functions;
use common\models\Status;
use common\models\user\UserLoginStatistic;
use Yii;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;
use cms\modules\v1\models\user\UserReal;

class UserController extends DefaultController
{
    public $modelClass = 'cms\modules\v1\models\user\User';
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [                     //需要过滤掉不用token参数的方法

                ]
            ]
        ]);
    }

    /* ====================== 用户列表 ========================= */
    public function actionList($page = 1, $size = 20) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new User())->getListData($page, $size);
        }
    }

    public function actionAddEdit($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        $model = new User();
        if (Yii::$app->request->isGet) {
            return $model->getInfo($id);
        }

        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if (isset($post['id']) && !empty($post['id'])) {
                $model->setScenario('editUser');
            } else {
                $model->setScenario('addUser');
            }
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->addEdit();
            }
        }
    }

    public function actionMultipleAdd() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return ( new User())->multipleAdd();
        }
    }

    public function actionField() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new User())->setField();
        }
    }

    public function actionUserRegister() {
        if (\Yii::$app->request->isGet) {
            return (new User())->weekRegister();
        }
    }

    //map
    public function actionUserMap () {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new User())->getSelectMap();
        }
    }

    public function actionClean() {
        if (Yii::$app->request->isPost) {
            return (new User())->cleanUser();
        }
    }

    /* ====================== 用户登录日志 ========================= */
    public function actionLoginLog($page = 1, $size = 20, $search = '') {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new UserLoginLog())->getListData($page, $size, $search);
        }
    }
    public function actionLoginStatistic($page = 1, $size = 20) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new UserLoginStatistic())->getList($page, $size);
        }
    }


    /* ====================== 用户实名认证 ========================= */
    //列表
    public function actionRealList($page = 1, $size = 20){
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new UserReal())->getList($page, $size);
        }
    }

    //实名审核
    public function actionRealExamine() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
    }

    /* ======================商户审核 ========================= */
    //列表
    public function actionBusinessList($page = 1, $size = 20){
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new UserBusiness())->getList($page, $size);
        }
    }

    //审核
    public function actionBusinessExamine() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new UserBusiness())->examineInfo();
        }
    }

    /* ====================== 用户异常记录 ========================= */
    //列表
    public function actionUnusualList($page = 1, $size = 20, $search = '') {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new UserUnusualLog())->getListData($page, $size, $search);
        }
    }

    //  status
    public function actionUnusualStatusMap() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return Functions::formatRestAPI(0, '', Status::USER_UNUSUAL_STATUS_MAP);
        }
    }

    // 处理type
    public function actionUnusualTypeMap() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return Functions::formatRestAPI(0, '', Status::USER_UNUSUAL_TYPE_MAP);
        }
    }

    //添加修改
    public function actionUnusualAddEdit($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        $model = new UserUnusualLog();
        if (Yii::$app->request->isGet) {
            return $model->getInfo($id);
        }

        if (Yii::$app->request->isPost) {
            return $model->addEdit();
        }
    }

    //封禁
    public function actionUnusual() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new UserUnusualLog())->saveUnusual();
        }
    }


}
